package com.study.jprotobuf_study;

import com.baidu.bjf.remoting.protobuf.*;
import com.study.jprotobuf_study.simple.SimpleType;
import com.study.jprotobuf_study.simple.SimpleTypeClase;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.IOException;
import java.util.Date;

@SpringBootApplication
public class JprotobufStudyApplication {

    public static void main(String[] args) {
        SpringApplication.run(JprotobufStudyApplication.class, args);

        String code = ProtobufIDLGenerator.getIDL(SimpleType.class);
        System.out.println(code);


        testClass();
//        testSimple();
    }

    public static void testSimple(){
        Codec<SimpleType> simpleType = ProtobufProxy.create(SimpleType.class);
        SimpleType stt = new SimpleType();
        stt.setName("abc testSimple");
        stt.setValue(100);
        try {
            // 序列化
            byte[] bb = simpleType.encode(stt);
            // 反序列化
            SimpleType newStt = simpleType.decode(bb);

//            System.out.println(newStt);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void testClass(){
        Codec<SimpleTypeClase> simpleTypeCodec = ProtobufProxy.create(SimpleTypeClase.class);
        SimpleTypeClase stt = new SimpleTypeClase();
        stt.setName("abc testClass");
        stt.setValue(100);
        try {

            long startTime = new Date().getTime();
            for(int i = 0; i < 10000000; i ++){
                // 序列化
                byte[] bb = simpleTypeCodec.encode(stt);
                // 反序列化
                SimpleTypeClase newStt = simpleTypeCodec.decode(bb);
            }
            long useTime = new Date().getTime() - startTime;
            System.out.println(useTime);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
